package cst.a228htt.recyclerviewtest228htt.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cst.a228htt.recyclerviewtest228htt.R;
import cst.a228htt.recyclerviewtest228htt.adapter.MyAdapter;
import cst.a228htt.recyclerviewtest228htt.bean.ArticleBean;
import cst.a228htt.recyclerviewtest228htt.iface.ArticleListener;
import cst.a228htt.recyclerviewtest228htt.model.ArticleModel;


public class MainActivity extends AppCompatActivity implements ArticleListener{
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter mAdapter;
    List<ArticleBean> list;

    private void init(){
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview1);
        //创建默认的线性LayoutManager
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        //创建并设置Adapter
//        list = getData();
        mAdapter = new MyAdapter(this);
        mAdapter.setList(list);
        recyclerView.setAdapter(mAdapter);
    }

    /*   private List<ArticleBean> getData(){
           List<ArticleBean> list = new ArrayList<ArticleBean>();
           ArticleBean art;
           art = new ArticleBean();
           art.setArticleId(1);
           art.setTitle("标题1");
           art.setImgid(R.drawable.f1);
           art.setTime("2017-5-12 15:16");
           art.setDescription("描述1");
           list.add(art);

           art = new ArticleBean();
           art.setArticleId(2);
           art.setTitle("标题2");
           art.setImgid(R.drawable.f2);
           art.setTime("2017-5-12 15:16");
           art.setDescription("描述2");
           list.add(art);

           art = new ArticleBean();
           art.setArticleId(3);
           art.setTitle("标题3");
           art.setImgid(R.drawable.f3);
           art.setTime("2017-5-12 15:16");
           art.setDescription("描述3");
           list.add(art);

           art = new ArticleBean();
           art.setArticleId(4);
           art.setTitle("标题4");
           art.setImgid(R.drawable.f4);
           art.setTime("2017-5-12 15:16");
           art.setDescription("描述4");
           list.add(art);

           art = new ArticleBean();
           art.setArticleId(5);
           art.setTitle("标题5");
           art.setImgid(R.drawable.f5);
           art.setTime("2017-5-12 15:16");
           art.setDescription("描述5");
           list.add(art);

           art = new ArticleBean();
           art.setArticleId(6);
           art.setTitle("标题6");
           art.setImgid(R.drawable.f6);
           art.setTime("2017-5-12 15:16");
           art.setDescription("描述6");
           list.add(art);

           return list;
       }*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        ArticleModel articleModel=new ArticleModel();
        articleModel.getResultList("article",2,"2ts2j910hv4m270hipde4a67d7",this);

    }

    @Override
    public void onResponse(List<ArticleBean> list) {
        this.list=list;
        mAdapter.setList(list);
        mAdapter.notifyDataSetChanged();//通知数据改变

    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, "Fail"+msg, Toast.LENGTH_SHORT).show();

    }
}
